package cn.south.campus.admin.campus.service.impl;

import cn.south.campus.admin.campus.mapper.GoodsCategoryMapper;
import cn.south.campus.admin.campus.model.entity.GoodsCategory;
import cn.south.campus.admin.campus.model.enums.CampusRespEnums;
import cn.south.campus.admin.campus.model.req.GoodsCategoryReq;
import cn.south.campus.admin.campus.model.vo.GoodsCategoryVo;
import cn.south.campus.admin.campus.service.GoodsCategoryService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import cn.south.campus.common.utils.HttpUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 二手商品分类表 服务实现类
 * </p>
 *
 * @author chow
 * @since 2024-11-13
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Value("${campus.url}")
    private String businessUrl;

    @Override
    public List<GoodsCategoryVo> tree(GoodsCategoryReq req) {
        LambdaQueryWrapper<GoodsCategory> wrapper = new LambdaQueryWrapper<GoodsCategory>().like(StringUtils.isNotBlank(req.getName()), GoodsCategory::getName, req.getName());
        List<GoodsCategory> categoryList = goodsCategoryMapper.selectList(wrapper);
        return buildCategory(categoryList, 0L, req.getName());
    }

    @Override
    public void add(GoodsCategoryVo vo) {
        GoodsCategory category = GoodsCategoryVo.convert(vo);
        goodsCategoryMapper.insert(category);
    }

    @Override
    public void delete(Long id) {
        //查询该菜单下面是否还有菜单
        GoodsCategory category = getById(id);
        AssertUtils.isNotNull(category, CommonEnums.DATA_HAS_NOT_ERROR);
        if (category.getParentId().equals(BigDecimal.ZERO.longValue())) {
            //查询该父级菜单下还有没有子菜单
            LambdaQueryWrapper<GoodsCategory> wrapper = new LambdaQueryWrapper<GoodsCategory>().eq(GoodsCategory::getParentId, id);
            List<GoodsCategory> list = goodsCategoryMapper.selectList(wrapper);
            AssertUtils.isNotBlankCollection(list, CampusRespEnums.CATEGORY_HAS_CHILD_ERROR);
        }
        goodsCategoryMapper.deleteById(id);
    }

    @Override
    public void updateCategory(GoodsCategoryVo vo) {
        GoodsCategory goodsCategory = getById(vo.getId());
        AssertUtils.isNotNull(goodsCategory, CommonEnums.DATA_HAS_NOT_ERROR);
        goodsCategory.setName(vo.getName());
        goodsCategory.setLevel(vo.getLevel());
        goodsCategory.setParentId(vo.getParentId());
        goodsCategory.setSort(vo.getSort());
        goodsCategoryMapper.updateById(goodsCategory);
    }

    @Override
    public GoodsCategoryVo detail(Long id) {
        GoodsCategory goodsCategory = goodsCategoryMapper.selectById(id);
        return GoodsCategoryVo.convert(goodsCategory);
    }

    @Override
    public void refresh() {
        HttpUtils.putReq(null, businessUrl + "/category/refresh");
    }


    private List<GoodsCategoryVo> buildCategory(List<GoodsCategory> categoryList, long parentId, String name) {
        //搜索条件
        if (StringUtils.isNotEmpty(name)) {
            for (GoodsCategory category : categoryList) {
                if (category.getParentId().equals(parentId)) {
                    return categoryList.stream().map(x -> getGoodsCategoryListVo(parentId, x)).collect(Collectors.toList());
                }
            }
            return categoryList.stream().filter(goodsCategory -> !goodsCategory.getParentId().equals(parentId)).
                    map(x -> getGoodsCategoryListVo(parentId, x)).collect(Collectors.toList());
        }
        //遍历所有 找出所有父级
        List<GoodsCategoryVo> list = categoryList.stream().
                filter(goodsCategory -> goodsCategory.getParentId().equals(parentId)).
                map(x -> getGoodsCategoryListVo(parentId, x)).collect(Collectors.toList());
        //遍历父级
        for (GoodsCategoryVo parentMenus : list) {
            List<GoodsCategoryVo> goodsCategoryListVos = buildCategory(categoryList, parentMenus.getId(), name);
            if (goodsCategoryListVos.size() > 0) {
                //将子加入到父级下面
                parentMenus.setChildren(goodsCategoryListVos);
            }
        }
        return list;
    }

    private GoodsCategoryVo getGoodsCategoryListVo(Long parentId, GoodsCategory x) {
        GoodsCategoryVo goodsCategoryVo = new GoodsCategoryVo();
        goodsCategoryVo.setName(x.getName());
        goodsCategoryVo.setLevel(x.getLevel());
        goodsCategoryVo.setSort(x.getSort());
        goodsCategoryVo.setId(x.getId());
        goodsCategoryVo.setParentId(parentId);
        return goodsCategoryVo;
    }
}
